<template>
  <div class="app-container">
    <!-- ****************************** -->
    <el-row :gutter="20" class="header" style="margin-left: 0">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item label="车型" prop="applicableModels">
          <el-select
            filterable
            v-model="queryParams.applicableModels"
            placeholder="所有项"
            clearable
            @change="currentCarType"
          >
            <el-option
              v-for="item in carData"
              :key="item.id"
              :label="item.applicableModels"
              :value="item.applicableModels"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="车号" prop="wagonNumber">
          <el-select
            filterable
            v-model="queryParams.wagonNumber"
            placeholder="所有项"
            clearable
          >
            <el-option
              v-for="item in currtentCarNumber"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="创建日期" prop="createTime">
          <el-date-picker
            v-model="dateRange"
            style="width: 240px"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </el-row>
    <!-- ****************************** -->
    <!-- 菜单列表 -->
    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="menuList"
      row-key="menuId"
      :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      style="width: 100%"
    >
      <el-table-column
        prop="id"
        label="ID"
        :show-overflow-tooltip="true"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="applicableModels"
        label="适用车型"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="wagonNumber"
        label="车号"
        align="center"
      ></el-table-column>
      <el-table-column prop="refuelingVolume" label="补加油量" align="center">
        <template slot-scope="scope">
          <span style="margin-right: 5px">{{ scope.row.refuelingVolume }}</span>
          <!-- <el-input v-show="edit" v-model="scope.row.refuelingVolume" style="width: 60px;"></el-input> -->
          <i
            :class="{ 'el-icon-edit': true }"
            @click="putRefuelingVolume(scope.row)"
          ></i>
        </template>
      </el-table-column>
      <el-table-column
        prop="updateBy"
        label="补加人"
        align="center"
      ></el-table-column>
      <el-table-column prop="updateTime" label="补加时间"></el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listMenu,
  getMenu,
  delMenu,
  addMenu,
  updateMenu,
} from "@/api/system/menu";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import IconSelect from "@/components/IconSelect";
import {
  checkList,
  getCarData,
  changeRefuelingVolume,
} from "@/api/system/check";
export default {
  name: "Menu",
  dicts: ["sys_show_hide", "sys_normal_disable"],
  components: { Treeselect, IconSelect },
  data() {
    return {
      /* 日期 start*/
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      // 日期范围
      dateRange: [],
      /* 日期 end*/
      //车辆信息
      carData: [],
      //当前车型的车号
      currtentCarNumber: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 菜单表格树数据
      menuList: [],
      // 菜单树选项
      menuOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部折叠
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applicableModels: "",
        dateView: "",
        isMe: false,
        type: 0,
        typeName: "",
        wagonNumber: "",
        startTime: "",
        endTime: "",
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
    this.getCarData();
  },
  activated() {
    this.getList();
    this.getCarData();
  },

  methods: {
    // 选择图标
    selected(name) {
      this.form.icon = name;
    },
    /** 查询菜单列表 */
    getList() {
      this.loading = true;
      /*start 请求数据转换*/
      if (this.dateRange && this.dateRange.length) {
        this.queryParams.startTime = this.dateRange[0];
        this.queryParams.endTime = this.dateRange[1];
      } else {
        this.queryParams.startTime = "";
        this.queryParams.endTime = "";
      }
      checkList(this.queryParams).then((response) => {
        this.menuList = this.handleTree(response.data.records, "id");
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 查询车辆数据 */
    getCarData() {
      getCarData().then((response) => {
        this.carData = response.data;
      });
    },
    //下拉框更新的车号
    currentCarType() {
      this.queryParams.wagonNumber = "";
      this.currtentCarNumber = this.carData.filter((item) => {
        return item.applicableModels == this.queryParams.applicableModels;
      })[0].applicableModelsData;
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.menuId,
        label: node.menuName,
        children: node.children,
      };
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      listMenu().then((response) => {
        this.menuOptions = [];
        const menu = { menuId: 0, menuName: "主类目", children: [] };
        menu.children = this.handleTree(response.data, "menuId");
        this.menuOptions.push(menu);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除名称为"' + row.menuName + '"的数据项？')
        .then(function () {
          return delMenu(row.menuId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    // 编辑补加油量
    putRefuelingVolume(row) {
      this.$prompt(`请输入车号为 ${row.wagonNumber} 的补加油量`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputValue: row.refuelingVolume,
        inputPattern:
          /^((0{1}\.\d{1,2})|([1-9]\d*\.{1}\d{1,2})|([1-9]+\d*)|0)$/,
        inputErrorMessage: "请输入数字(支持两位小数)",
      })
        .then(({ value }) => {
          console.log(value, row);
          let data = {};
          data.id = row.id;
          data.refuelingVolume = value;
          changeRefuelingVolume(data).then(() => {
            //刷新菜单
            this.getList();
            this.$message({
              type: "success",
              message: "修改成功",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
      // this.edit = !this.edit
    },
  },
};
</script>
<style></style>
